﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceManager.DataAccessLayer
{
    public class ComponentDbOperation:IDatabaseOperation<PHUTUNG>
    {
        QLThietBiEntities devicemanager;
        public int SaveRecord(PHUTUNG record)
        {
            try
            {
                using (devicemanager = new QLThietBiEntities())
                {
                    devicemanager.PHUTUNGs.Add(record);
                    devicemanager.SaveChanges();
                }
                return (int)record.PhuTungID;
            }
            catch
            {
                return -1;
            }
        }

        public bool UpdateRecord(PHUTUNG record)
        {
            using (devicemanager = new QLThietBiEntities())
            {
                devicemanager.PHUTUNGs.Find(record.PhuTungID);
                devicemanager.SaveChanges();
                return true;
            }
        }

        public List<PHUTUNG> GetRecord(PHUTUNG record)
        {
            devicemanager = new QLThietBiEntities();
            IQueryable<PHUTUNG> custQuery = devicemanager.PHUTUNGs.AsQueryable<PHUTUNG>();
            if (!string.IsNullOrEmpty(record.MaPhuTung))
                custQuery = custQuery.Where(c => c.MaPhuTung.Contains(record.MaPhuTung));
            if (!string.IsNullOrEmpty(record.TenPhuTung))
                custQuery = custQuery.Where(c => c.TenPhuTung.Contains(record.TenPhuTung));
            return custQuery.ToList();
        }

        public bool DeleteRecord(PHUTUNG record)
        {
            using (devicemanager = new QLThietBiEntities())
            {
                var cust = devicemanager.PHUTUNGs.FirstOrDefault(c => c.PhuTungID == record.PhuTungID);
                devicemanager.PHUTUNGs.Remove(cust);
                devicemanager.SaveChanges();
                return true;
            }
        }
    }
}
